package util;

import java.util.HashSet;
import java.util.Set;

public class Random {

	private static final int DEFAULT_MIN_VALUE = 1;
	private static final int DEFAULT_MAX_VALUE = 99;
	private static final int DEFAULT_ARRAY_LENGTH = 20;
	private static boolean isDistinct;
	//private static enum DEFAULT_ORDER // TBD


	// generate a random number between min and max value inclusive both
	// min and max can even be negative
	public static int generateARandomNumber(int min, int max){

		// if min > max, swap them
		if(min > max){
			min = min ^ max;
			max = min ^ max;
			min = min ^ max;			
		}

		if(max >=0){ // case 1
			return (int)((Math.random())*(max-min+1)) + min;
		}else{ // case 2
			return (int)((Math.random())*(min-max-1)) + max;
		}
	}


	public static int[] generateAnArray(){
		return generateAnArray(DEFAULT_ARRAY_LENGTH, DEFAULT_MIN_VALUE, DEFAULT_MAX_VALUE, true);
	}

	
	public static int[] generateAnArray(int length){
		return generateAnArray(length, DEFAULT_MIN_VALUE, DEFAULT_MAX_VALUE, true);
	}

	
	public static int[] generateAnArray(int legnth, int min, int max, boolean isDistinct){

		// corner case TBD: in case of distinct values, length should be like that prg does terminate
		
		// optimization TBD

		int[] arr = new int[legnth];

		int i = 0;
		int randomNumber = 0;

		// used in case of Values.DISTINCT only	
		Set<Integer> uniqueSet = new HashSet<Integer>(); // disadvantage of autoboxing

		if(isDistinct == true){
			while(i < arr.length){
				randomNumber = generateARandomNumber(min, max);
				if(!uniqueSet.contains(randomNumber)){
					uniqueSet.add(randomNumber);
					arr[i++] = randomNumber;
				}
			}
		}else{
			while(i < arr.length){
				randomNumber = generateARandomNumber(min, max);
				arr[i++] = randomNumber;
			}
		}

		return arr;
	}
}
